package c.x.jy.service.service.impl;

import c.x.jy.dao.dto.TeacherConfigEntityDto;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import c.x.jy.common.utils.PageUtils;
import c.x.jy.common.utils.Query;

import c.x.jy.dao.dao.TeacherConfigDao;
import c.x.jy.dao.entity.TeacherConfigEntity;
import c.x.jy.service.service.TeacherConfigService;
import org.apache.commons.lang3.StringUtils;


@Service("teacherConfigService")
public class TeacherConfigServiceImpl extends ServiceImpl<TeacherConfigDao, TeacherConfigEntity> implements TeacherConfigService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        	QueryWrapper<TeacherConfigEntity> query= 	 new QueryWrapper<TeacherConfigEntity>() ;
    	if(params.get("searchKey")!=null&&params.get("searchValue")!=null&&!StringUtils.isEmpty(params.get("searchKey").toString())&&!StringUtils.isEmpty(params.get("searchValue").toString()))
    	    query.eq(  params.get("searchKey").toString(), params.get("searchValue"));
        if(params.get("businessId")!=null) 	query.eq("business_id", params.get("businessId"));
        IPage<TeacherConfigEntity> page = this.page(
                new Query<TeacherConfigEntity>().getPage(params),
                 query
        );

        return new PageUtils(page);
    }

    @Override
    public TeacherConfigEntity getCurrentTeacherConfig(Long memberId,Long teacherId) {
        QueryWrapper<TeacherConfigEntity> query= 	 new QueryWrapper<TeacherConfigEntity>() ;
        if(teacherId!=null) 	query.lambda().eq(TeacherConfigEntity::getTeacherId  ,teacherId);
        TeacherConfigEntity teacherConfigEntity = this.getBaseMapper().selectOne(query);
        if(teacherConfigEntity==null){
            teacherConfigEntity = new TeacherConfigEntity();
            teacherConfigEntity.setTeacherId(teacherId);
            teacherConfigEntity.setMemberId(memberId);
            teacherConfigEntity.setAskPrice(new BigDecimal(0));
            teacherConfigEntity.setPhonePrice(new BigDecimal(0));
            teacherConfigEntity.setJoinQuickAsk(0);
            teacherConfigEntity.setAcceptFreeReask(0);
            teacherConfigEntity.setFreeReaskTimes(0);
            this.getBaseMapper().insert(teacherConfigEntity);
        }
        return teacherConfigEntity;
    }

	@Override
	public TeacherConfigEntity getByTeacherId(Long teacherId) {
	     QueryWrapper<TeacherConfigEntity> query= 	 new QueryWrapper<TeacherConfigEntity>() ;
	     query.lambda().eq(TeacherConfigEntity::getTeacherId, teacherId);
		return    this.getBaseMapper().selectOne(query);
	}

    @Override
    public TeacherConfigEntity getByMemberId(Long memberId) {
        QueryWrapper<TeacherConfigEntity> query=new QueryWrapper<>();
        query.lambda().eq(TeacherConfigEntity::getMemberId,memberId);
        query.last("limit 1");
        return this.getBaseMapper().selectOne(query);
//        return    this.getBaseMapper().getByMemberId(memberId);
    }

    @Override
    public PageUtils listforweb(Map<String, Object> params) {
        QueryWrapper<TeacherConfigEntity> query= 	 new QueryWrapper<TeacherConfigEntity>() ;
        if(params.get("searchKey")!=null&&params.get("searchValue")!=null&&!StringUtils.isEmpty(params.get("searchKey").toString())&&!StringUtils.isEmpty(params.get("searchValue").toString()))
            query.eq(  params.get("searchKey").toString(), params.get("searchValue"));
        if(params.get("businessId")!=null) 	query.eq("business_id", params.get("businessId"));
        IPage<TeacherConfigEntityDto> page = this.getBaseMapper().listforweb(
                new Query<TeacherConfigEntity>().getPage(params),
                query
        );

        return new PageUtils(page);
    }
}
